﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Einstein.Utility
{
    public class OfflineBoardAPI
    {
        public static readonly int DefaultPort = 6767;

        /// <summary>
        /// 打开网络 建立通迅
        /// </summary>
        /// <returns>int</returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_open_Net")]
        public static extern int OpenNet();

        /// <summary>
        /// 关闭网络
        /// </summary>
        /// <returns>int</returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Close_Net")]
        public static extern int CloseNet();

        /// <summary>
        /// 设置网络包的延时
        /// </summary>
        /// <returns>int</returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_TimeOut")]
        public static extern int SetTimeout(int aDelay);

        /// <summary>
        /// 绑定网络
        /// </summary>
        /// <param name="szServer">目标地址</param>
        /// <param name="port">目标端口</param>
        /// <returns>int </returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Udp_bind")]
        public static extern int UdpBind(StringBuilder szServer, int port);

        /// <summary>
        /// 修改IP地址
        /// </summary>
        /// <param name="aNetIP">参考结构说明，需要修改成的网络参数</param>
        /// <param name="ip">需要操作的设备IP</param>
        /// <param name="port">需要操作的设备的端口，固定6767</param>
        /// <returns>int</returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Addr")]
        public static extern int SetAddress(T_NetIP aNetIP, StringBuilder ip, int port);

        /// <summary>
        /// 设置时间
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口</param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Time")]
        public static extern int SetTime(StringBuilder ip, int port);

        /// <summary>
        /// 初始化机器
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口</param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_IniEqu")]
        public static extern int Init(StringBuilder ip, int port);

        /// <summary>
        /// 初始化机器
        /// </summary>
        /// <param name="aEnable">标志</param>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口</param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Rec_Cyc")]
        public static extern int SetRecCyc(byte aEnable, StringBuilder ip, int port);

        /// <summary>
        /// 清除记录
        /// </summary>
        /// <param name="aEnable">标志</param>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口</param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Clear_Rec")]
        public static extern int ClearRec(byte aEnable, StringBuilder ip, int port);

        /// <summary>
        /// 恢复记录采集指针
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口</param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Restore_Rec_Point")]
        public static extern int RestoreRecPoint(StringBuilder ip, int port);

        /// <summary>
        /// 注册单卡
        /// </summary>
        /// <param name="aUserReg"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Restore_Rec_Point")]
        public static extern int RegistCard(TUserReg aUserReg, StringBuilder ip, int port);

        /// <summary>
        /// 读单卡信息，只需要带入原始卡号
        /// </summary>
        /// <param name="aUserReg">卡注册结构体</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_One_Card")]
        public static extern int ReadOneCard(ref TUserReg aUserReg, StringBuilder ip, int port);

        /// <summary>
        /// 读flash配置信息
        /// </summary>
        /// <param name="aFlashInfo">存储器参数结构体</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Flash_Info")]
        public static extern int ReadFlashInfo(ref TFlashInfo aFlashInfo, StringBuilder ip, int port);

        /// <summary>
        /// 删除单卡
        /// </summary>
        /// <param name="aUserReg">卡注册结构体</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Delete_Card")]
        public static extern int DeleteCard(TUserReg aUserReg, StringBuilder ip, int port);

        /// <summary>
        /// 下发门禁时间表
        /// </summary>
        /// <param name="aGroupId"></param>
        /// <param name="aDoorTimeGroup"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_DoorTimeGroup")]
        public static extern int SetDoorTimeGroup(byte aGroupId, TDoorTimeGroup aDoorTimeGroup, StringBuilder ip, int port);

        /// <summary>
        /// 下发节假日
        /// </summary>
        /// <param name="aHolidays"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Holidays")]
        public static extern int SetHolidays(THolidays aHolidays, StringBuilder ip, int port);

        /// <summary>
        /// 设置出入口独立开门密码
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="aPass">密码</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Channel_OpenDoorPass")]
        public static extern int SetChannelOpenDoorPass(byte aChannel, StringBuilder aPass, StringBuilder ip, int port);

        /// <summary>
        /// 读出入口独立开门密码
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="aPass"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Channel_OpenDoorPass")]
        public static extern int ReadChannelOpenDoorPass(byte aChannel, ref StringBuilder aPass, StringBuilder ip, int port);

        /// <summary>
        /// 设置胁迫开门密码
        /// </summary>
        /// <param name="aPass"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Intimidate_Pass")]
        public static extern int SetIntimidatePass(StringBuilder aPass, StringBuilder ip, int port);

        /// <summary>
        /// 设置出入口控制参数
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="aChannelParam"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Channel_Param")]
        public static extern int SetChannelParam(byte aChannel, ref TChannelParam aChannelParam, StringBuilder ip, int port);

        /// <summary>
        /// 设置出入口报警控制参数
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="aChannelWarnParam"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Channel_Warn_Param")]
        public static extern int SetChannelWarnParam(byte aChannel, TChannelWarnParam aChannelWarnParam, StringBuilder ip, int port);

        /// <summary>
        /// 设置出入口时长，开门时长、等待进入、开门超时、延时报警
        /// </summary>
        /// <param name="aOpenDoor"></param>
        /// <param name="aWaitEntry"></param>
        /// <param name="aTimeout"></param>
        /// <param name="warnDelay"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Time_Param")]
        public static extern int SetTimeParam(byte aOpenDoor, byte aWaitEntry, byte aTimeout, byte warnDelay, StringBuilder ip, int port);

        /// <summary>
        /// 远程开启出入口，出入口号、是否自动关闭
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="aManualClose"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Open_Channel")]
        public static extern int OpenChannel(byte aChannel, bool aManualClose, StringBuilder ip, int port);

        /// <summary>
        /// 远程关闭出入口，出入口号、常闭
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="AlwaysClose"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Close_Channel")]
        public static extern int CloseChannel(byte aChannel, bool AlwaysClose, StringBuilder ip, int port);

        /// <summary>
        /// 打开出入口现场报警
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Open_Channel_Warn")]
        public static extern int OpenChannelWarn(byte aChannel, StringBuilder ip, int port);

        /// <summary>
        /// 清除出入口现场报警
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Clear_Channel_Warn")]
        public static extern int ClearChannelWarn(byte aChannel, StringBuilder ip, int port);

        /// <summary>
        /// 读机号
        /// </summary>
        /// <param name="aNetIP"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_EquNo")]
        public static extern int ReadEquNO(ref T_NetIP aNetIP, StringBuilder ip, int port);

        /// <summary>
        /// 读版本号
        /// </summary>
        /// <param name="version"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Ver")]
        public static extern int ReadVer(ref StringBuilder version, StringBuilder ip, int port);

        /// <summary>
        /// 读时间
        /// </summary>
        /// <param name="aTime"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Time")]
        public static extern int ReadTime(ref StringBuilder aTime, StringBuilder ip, int port);

        /// <summary>
        /// 启动续读
        /// </summary>
        /// <param name="aRecordCount"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Entry_Data_Collect")]
        public static extern int EntryDataCollect(ref int aRecordCount, StringBuilder ip, int port);

        /// <summary>
        /// 读当前  首先要开启续读
        /// </summary>
        /// <param name="aRecord"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Current_Rec")]
        public static extern int ReadCurrentRec(ref TRecord aRecord, StringBuilder ip, int port);

        /// <summary>
        /// 读下一条   首先要开启续读
        /// </summary>
        /// <param name="aRecord"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Next_Rec")]
        public static extern int ReadNextRec(ref TRecord aRecord, StringBuilder ip, int port);

        /// <summary>
        /// 退出续读   与上面三条命令组合使用
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Exit_Data_Collect")]
        public static extern int ExitDataCollect(StringBuilder ip, int port);

        /// <summary>
        /// 读门禁时间组  最多64张
        /// </summary>
        /// <param name="aGroupId"></param>
        /// <param name="aDoorTimeGroup"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_DoorTimeGroup")]
        public static extern int ReadDoorTimeGroup(byte aGroupId, ref TDoorTimeGroup aDoorTimeGroup, StringBuilder ip, int port);

        /// <summary>
        /// 读节假日
        /// </summary>
        /// <param name="aHolidays"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Holidays")]
        public static extern int ReadHolidays(ref THolidays aHolidays, StringBuilder ip, int port);

        /// <summary>
        /// 读互锁
        /// </summary>
        /// <param name="aChannelID"></param>
        /// <param name="aMutexLock"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_MutexLock")]
        public static extern int ReadMutexLock(byte aChannelID, ref TMutexLock aMutexLock, StringBuilder ip, int port);

        /// <summary>
        /// 设置互锁
        /// </summary>
        /// <param name="aChannelID"></param>
        /// <param name="aMutexLock"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_MutexLock")]
        public static extern int SetMutexLock(byte aChannelID, ref TMutexLock aMutexLock, StringBuilder ip, int port);

        /// <summary>
        /// 读胁迫码
        /// </summary>
        /// <param name="aPass"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Intimidate_Pass")]
        public static extern int ReadIntimidatePass(ref StringBuilder aPass, StringBuilder ip, int port);

        /// <summary>
        /// 读出入口控制参数
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="aChannelParam"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Channel_Param")]
        public static extern int ReadChannelParam(byte aChannel, ref TChannelParam aChannelParam, StringBuilder ip, int port);

        /// <summary>
        /// 读出入口报警参数
        /// </summary>
        /// <param name="aChannel"></param>
        /// <param name="aChannelWarnParam"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Channel_Warn_Param")]
        public static extern int ReadChannelWarnParam(byte aChannel, ref TChannelWarnParam aChannelWarnParam, StringBuilder ip, int port);

        /// <summary>
        /// 读时长参数
        /// </summary>
        /// <param name="aOpenDoor"></param>
        /// <param name="aTimeOut"></param>
        /// <param name="aWarnDelay"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Time_Param")]
        public static extern int ReadTimeParam(ref byte aOpenDoor, ref byte aTimeOut, ref byte aWarnDelay, StringBuilder ip, int port);

        /// <summary>
        /// 读设备状态
        /// </summary>
        /// <param name="deviceStatus"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_DeviceStatus")]
        public static extern int ReadDeviceStatus(ref TDeviceStatus deviceStatus, StringBuilder ip, int port);

        /// <summary>
        /// 读记录循环存放标志
        /// </summary>
        /// <param name="cycFlag"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Rec_Cyc")]
        public static extern int ReadRecCyc(ref int cycFlag, StringBuilder ip, int port);

        /// <summary>
        /// 设置设备时间组
        /// </summary>
        /// <param name="aChannelTimeGroup"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Channel_DeptTime")]
        public static extern int SetChannelDeptTime(ref TChannelTimeGroup aChannelTimeGroup, StringBuilder ip, int port);

        /// <summary>
        /// 读设备时间组
        /// </summary>
        /// <param name="aChannelTimeGroup"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Channel_DeptTime")]
        public static extern int ReadChannelDeptTime(ref TChannelTimeGroup aChannelTimeGroup, StringBuilder ip, int port);

        /// <summary>
        /// 设置其它时间组
        /// </summary>
        /// <param name="aChannelOtherTime"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Channel_OtherTime")]
        public static extern int SetChannelOtherTime(ref TChannelOtherTime aChannelOtherTime, StringBuilder ip, int port);

        /// <summary>
        /// 读其它时间组
        /// </summary>
        /// <param name="aChannelOtherTime"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Channel_OtherTime")]
        public static extern int ReadChannelOtherTime(ref TChannelOtherTime aChannelOtherTime, StringBuilder ip, int port);

        /// <summary>
        /// 读设置反回转标志  本地反回转、全局反回转
        /// </summary>
        /// <param name="aLoacalPassBack"></param>
        /// <param name="aGlobalPassBack"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_PASS_BACK")]
        public static extern int SetPassBack(int aLoacalPassBack, int aGlobalPassBack, StringBuilder ip, int port);

        /// <summary>
        /// 读反回转标志  本地反回转、全局反回转
        /// </summary>
        /// <param name="aLoacalPassBack"></param>
        /// <param name="aGlobalPassBack"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Pass_Back")]
        public static extern int ReadPassBack(ref int aLoacalPassBack, ref int aGlobalPassBack, StringBuilder ip, int port);

        /// <summary>
        /// 读记录数信息
        /// </summary>
        /// <param name="aRecCountInfo"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_RecCnt_Info")]
        public static extern int ReadRecCntInfo(ref TRecCountInfo aRecCountInfo, StringBuilder ip, int port);

        /// <summary>
        /// 读下一条记录，可以单独调用
        /// </summary>
        /// <param name="aRecord"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_One_Next_Rec")]
        public static extern int ReadOneNextRec(ref TRecord aRecord, StringBuilder ip, int port);

        /// <summary>
        /// 读当前记录，可以单独调用
        /// </summary>
        /// <param name="aRecord"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_One_Current_Rec")]
        public static extern int ReadOneCurrentRec(ref TRecord aRecord, StringBuilder ip, int port);

        /// <summary>
        /// 设置实时验证服务器
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Set_Server")]
        public static extern int SetServer(StringBuilder ip, int port);

        /// <summary>
        /// 下发LED屏显示信息,MSG最长10位
        /// </summary>
        /// <param name="LedNo">屏号(1,2)	接串口1的为1号屏，接串口2的为2号屏</param>
        /// <param name="Msg"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_RealLedMsg")]
        public static extern int RealLedMsg(int LedNo, StringBuilder Msg, StringBuilder ip, int port);

        /// <summary>
        /// 读实时刷卡信息
        /// </summary>
        /// <param name="rectype">记录类型，0为脱机刷卡记录，此时aRecord有效；1为实时请求记录，此时ReaderNo,CardNO有效</param>
        /// <param name="aRecord"></param>
        /// <param name="ReaderNo">读头号</param>
        /// <param name="CardNO">卡号  10位ABA号</param>
        /// <param name="clientIP">请求实时处理的门禁机IP</param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Read_Card")]
        public static extern int ReadCard(ref int rectype, ref TRecord aRecord, ref int ReaderNo, ref StringBuilder CardNO, ref StringBuilder clientIP);

        /// <summary>
        /// 注册实时验证卡
        /// </summary>
        /// <param name="CardId"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Reg_RealCard")]
        public static extern int RegRealCard(int CardId, StringBuilder ip, int port);

        /// <summary>
        /// 删除实时验证卡
        /// </summary>
        /// <param name="CardId"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX104Net.dll", EntryPoint = "JX104Net_Del_RealCard")]
        public static extern int DelRealCard(int CardId, StringBuilder ip, int port);


    }
}
